﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CLFramework.Infrastructure
{
    /// <summary>
    /// 分页信息
    /// </summary>
    public class PagerInfo
    {

        public PagerInfo()
        {
            Header = "<div class=\"pager\">";
            Footer = "</div>";
            PageIndex = 0;
            PageSize = 20;
            TotalPageCount = 0;
            IndividualPagesDisplayedCount = 10;
            PreviousLinkFormat = "<a href=\"{url}\" class=\"previous\">Previous</a>";
            PreviousText = "&lt;&lt;";
            AlwaysShowPrevious = false;
            NextLinkFormat = "<a href=\"{url}\" class=\"next\">Next</a>";
            NextText = "&gt;&gt;";
            AlwaysShowNext = false;
            FirstLinkFormat = "<a href=\"{url}\" class=\"first\">First</a>";
            //FirstText = "First";
            ShowFirst = true;
            ShowFirstPagesCount = 0;
            LastLinkFormat = "<a href=\"{url}\" class=\"last\">Last</a>";
            //LastText = "Last";
            ShowLast = true;
            ShowLastPagesCount = 0;
            PageLinkFormat = "<a href=\"{url}\" class=\"page\">{page}</a>";
            CurrentPageFormat = "<span>{0}</span>";
            Separator = " , ";
            Ellipsis = "<span>&#8230;</span>";
            PageStateFormat = "<span>Page {currentPage} of {totalPageCount} (Showing {fromIndex} to {toIndex} of {totalRecordCount})</span>";
            TotalRecordCount = 0;
        }

        /// <summary>
        /// 页码信息
        /// 如：当前第12页，共819页，共16376条记录
        /// </summary>
        public string PageState
        {
            get {
                string pageState = null;
                if (string.IsNullOrEmpty(PageStateFormat))
                    return PageStateFormat;
                int toIndex = ((PageIndex + 1) * PageSize) + 1;
                pageState = PageStateFormat;
                pageState = pageState.Replace("{currentPage}", (PageIndex + 1).ToString());
                pageState = pageState.Replace("{totalPageCount}", TotalPageCount.ToString());
                pageState = pageState.Replace("{fromIndex}", ((PageIndex * PageSize) + 1).ToString());
                pageState = pageState.Replace("{toIndex}", (toIndex > TotalRecordCount ? TotalRecordCount : toIndex).ToString());
                pageState = pageState.Replace("{totalRecordCount}", TotalRecordCount.ToString());
                return pageState;                
            }
        }

        /// <summary>
        /// 格式化生成最终链接的HTML格式
        /// </summary>
        /// <param name="format"></param>
        /// <param name="url"></param>
        /// <param name="text"></param>
        /// <returns></returns>
        string FormatLink(string format, string url, string text)
        {
            if (string.IsNullOrEmpty(format))
                return format;
            format = format.Replace("{url}", url);
            format = format.Replace("{page}", text);
            return format;
        }

        /// <summary>
        /// 获取"首页"链接HTML
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public string GetFirstLink(string url)
        {
            return FormatLink(FirstLinkFormat, url, "");
        }

        /// <summary>
        /// 获取"末页"链接HTML
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public string GetLastLink(string url)
        {
            return FormatLink(LastLinkFormat, url, "");
        }

        /// <summary>
        /// 获取"上一页"链接HTML
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public string GetPreviousLink(string url)
        {
            return FormatLink(PreviousLinkFormat, url, "");
        }

        /// <summary>
        /// 获取"下一页"链接Html
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public string GetNextLink(string url)
        {
            return FormatLink(NextLinkFormat, url, "");
        }

        /// <summary>
        /// 获取页码链接Html
        /// </summary>
        /// <param name="url"></param>
        /// <param name="pageIndex"></param>
        /// <returns></returns>
        public string GetPageLink(string url, int pageIndex)
        {
            return FormatLink(PageLinkFormat, url, (pageIndex + 1).ToString());
        }

        /// <summary>
        /// 获取当前页码HTML
        /// </summary>
        public string CurrentPageText
        { 
            get
            {
                return string.Format(CurrentPageFormat, PageNumber);
            }
        }

        public string Header { get; set; }

        public string Footer { get; set; }

        /// <summary>
        /// 当前页索引
        /// 从0开始
        /// </summary>
        public int PageIndex { get; set; }

        /// <summary>
        /// 每页大小
        /// </summary>
        public int PageSize { get; set; }

        /// <summary>
        /// 总页数
        /// </summary>
        public int TotalPageCount { get; set; }

        /// <summary>
        /// 中间显示的页码数量
        /// </summary>
        public int IndividualPagesDisplayedCount { get; set; }

        /// <summary>
        /// "上一页"链接格式
        /// 链接Url用{url}代替，链接文本用{page}代替
        /// </summary>
        public string PreviousLinkFormat { get; set; }

        /// <summary>
        /// "上一页"文本
        /// </summary>
        public string PreviousText { get; set; }

        /// <summary>
        /// 是否一直显示"上一页"链接
        /// true: 一直显示
        /// false: 如果当前页码是第一页，则不显示
        /// </summary>
        public bool AlwaysShowPrevious { get; set; }

        /// <summary>
        /// "下一页"链接格式
        /// 链接Url用{url}代替，链接文本用{page}代替
        /// </summary>
        public string NextLinkFormat { get; set; }

        /// <summary>
        /// "下一页"文本
        /// </summary>
        public string NextText { get; set; }

        /// <summary>
        /// 是否一直显示"下一页"链接
        /// true: 一直显示
        /// false: 如果当前页码是最后一页，则不显示
        /// </summary>
        public bool AlwaysShowNext { get; set; }

        /// <summary>
        /// "首页"链接格式
        /// 链接Url用{url}代替，链接文本用{page}代替
        /// </summary>
        public string FirstLinkFormat { get; set; }

        /// <summary>
        /// "首页"文本
        /// </summary>
        //public string FirstText { get; set; }

        /// <summary>
        /// 是否显示"首页"链接
        /// true: 显示
        /// flase: 不显示
        /// </summary>
        public bool ShowFirst { get; set; }

        /// <summary>
        /// 显示最开始若干页码的数量
        /// 0表示不显示
        /// </summary>
        public int ShowFirstPagesCount { get; set; }

        /// <summary>
        /// "末页"链接格式
        /// 链接Url用{url}代替，链接文本用{page}代替
        /// </summary>
        public string LastLinkFormat { get; set; }

        /// <summary>
        /// "末页"链接文本
        /// </summary>
        //public string LastText { get; set; }

        /// <summary>
        /// 是否显示"末页"链接
        /// true: 显示
        /// flase: 不显示
        /// </summary>
        public bool ShowLast { get; set; }

        /// <summary>
        /// 显示最后若干页码的数量
        /// 0表示不显示
        /// </summary>
        public int ShowLastPagesCount { get; set; }

        /// <summary>
        /// 页码链接格式
        /// 链接Url用{url}代替，链接文本用{page}代替
        /// </summary>
        public string PageLinkFormat { get; set; }

        /// <summary>
        /// 当前页码格式
        /// {0}表示当前页码
        /// </summary>
        public string CurrentPageFormat { get; set; }

        /// <summary>
        /// 页码之间的分隔符号
        /// </summary>
        public string Separator { get; set; }

        /// <summary>
        /// 多余页码省略号代替
        /// </summary>
        public string Ellipsis { get; set; }

        /// <summary>
        /// 页码信息
        /// {currentPage}: 当前页码
        /// {totalPageCount}: 总页数
        /// {fromIndex}: 当前页记录开始
        /// {toIndex}: 当前页记录结束
        /// {totalRecordCount}: 总记录数
        /// 例如: Page {currentPage} of {totalPageCount} (Showing {fromIndex} to {toIndex} of {totalRecordCount})
        /// </summary>
        public string PageStateFormat { get; set; }

        /// <summary>
        /// 总记录条数
        /// </summary>
        public int TotalRecordCount { get; set; }

        /// <summary>
        /// 当前页码
        /// </summary>
        public int PageNumber
        {
            get {
                return PageIndex + 1;
            }
        }

        /// <summary>
        /// "上一页"页码
        /// </summary>
        public int PreviousPageNumber
        {
            get {
                return PageIndex < 1 ? 1 : PageIndex;
            }
        }

        /// <summary>
        /// "下一页"页码
        /// </summary>
        public int NextPageNumber
        {
            get {
                return PageIndex + 2 > TotalPageCount ? TotalPageCount : PageIndex + 2;
            }
        }

        /// <summary>
        /// "首页"页码
        /// </summary>
        public int FirstPageNumber
        {
            get {
                return 1;
            }
        }

        /// <summary>
        /// "末页"页码
        /// </summary>
        public int LastPageNumber
        {
            get {
                return TotalPageCount > 0 ? TotalPageCount : 1;
            }
        }

        /// <summary>
        /// 中间显示的页码集合的开始页索引
        /// </summary>
        public int FirstIndividualPageIndex
        {
            get
            {
                if (TotalPageCount < IndividualPagesDisplayedCount || PageIndex - (IndividualPagesDisplayedCount / 2) < 0)
                    return 0;
                else if (PageIndex + (IndividualPagesDisplayedCount / 2) >= TotalPageCount)
                    return TotalPageCount - IndividualPagesDisplayedCount;
                else
                    return PageIndex - (IndividualPagesDisplayedCount / 2);
            }
        }

        /// <summary>
        /// 中间显示的页码集合的结束页索引
        /// </summary>
        public int LastIndividualPageIndex
        {
            get
            {
                int lastBuffer = IndividualPagesDisplayedCount / 2;
                if (IndividualPagesDisplayedCount % 2 == 0)
                    lastBuffer--;

                if (TotalPageCount < IndividualPagesDisplayedCount || PageIndex + lastBuffer >= TotalPageCount)
                    return TotalPageCount - 1;
                else if (PageIndex - (IndividualPagesDisplayedCount / 2) < 0)
                    return IndividualPagesDisplayedCount - 1;
                else
                    return PageIndex + lastBuffer;
            }
        }
    }
}
